<?php

//require 访问控制类
require_once 'AccessControl.php';
require_once 'Validator.php';

class LanguageController extends Zend_Controller_Action
{

    public function init()
    {
        $this->access = new AccessControl(); //实例化访问控制类
        $this->Validator = new Validator(); //实例化验证类
    }

    public function indexAction()
    {
        // action body
    }

    //语言列表
    public function listAction()
    {
        if($this->access->isAdmin())
        {
            $languageDb = new Application_Model_DbTable_Language();
            $this->view->languages = $languageDb->getAllLanguages();
        }
        
    }

    //更改语言信息
    public function updateAction()
    {
        if(!$this->access->isAdmin())
        {
            $this->view->ErrorInfo = Validator::PermissionDenied;
            return;
        }
        
        $id = $this->getRequest()->getUserParam('lid');
        if (isset($id))
        {
            $languageDb = new Application_Model_DbTable_Language();
            $language = $languageDb->getLanguageById($id);
            
            $languageInfo =new Application_Form_LanguageInfo();
            $languageInfo->lname->setValue($language->getName());
            $languageInfo->lcompiler_name->setValue($language->getCompiler());
            $languageInfo->lcompiler_path->setValue($language->getCompilerPath());
            $languageInfo->lcompiler_args->setValue($language->getCompileArgs());
            
            $this->view->languageInfo = $languageInfo;
            
            if($this->getRequest()->isPost())
            {
                $post = $this->getRequest()->getPost();
                
                if(!$languageInfo->isValid($post))
                    return;
                
                $language->setName($post['lname']);
                $language->setCompiler($post['lcompiler_name']);
                $language->setCompilerPath($post['lcompiler_path']);
                $language->setCompileArgs($post['lcompiler_args']);
                
                $languageDb->updateLanguage($language); //写入数据库
                
                $this->_redirect('/Language/list'); //转到语言列表
            }
        }
        
    }

    //添加支持语言
    public function addAction()
    {
        if(!$this->access->isAdmin())
        {
            $this->view->ErrorInfo = Validator::PermissionDenied;
            return;
        }
        
        $LanguageInfo = new Application_Form_LanguageInfo();
        
        $this->view->LanguageInfo = $LanguageInfo;
        
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost(); //POST 信息数组
            
            if(!$LanguageInfo->isValid($post))
                return;
            
            $language = new Application_Model_Language();
            $language->setName($post['lname']);
            $language->setCompiler($post['lcompiler_name']);
            $language->setCompilerPath($post['lcompiler_path']);
            $language->setCompileArgs($post['lcompiler_args']);
            
            $LanguageDb = new Application_Model_DbTable_Language();
            $LanguageDb->addLanguage($language);
            
            $this->_redirect('/Language/list'); //添加成功后跳转
        }
    }

}







